package com.swsc.xapp.offline.lhttpd;

import com.swsc.xapp.model.DefectAddReqVO;
import com.swsc.xapp.model.DefectDetailResVO;
import com.swsc.xapp.model.DefectEditReqVO;
import com.swsc.xapp.model.DefectMarkerResVO;
import com.swsc.xapp.model.DefectQueryReqVO;
import com.swsc.xapp.model.DefectResVO;
import com.swsc.xapp.model.DefectStatisticsResVO;
import com.swsc.xapp.model.DefectTableHeaderConditionResVO;
import com.swsc.xapp.model.PageInfo;
import com.swsc.xapp.model.RestResp;
import com.swsc.xapp.model.StationDefectResVO;
import com.swsc.xapp.model.StationSimulateMapDrawSelectionReqVO;
import com.swsc.xapp.model.geo.GeoJson;
import com.swsc.xapp.xroom.OfflineBizService;
import com.yanzhenjie.andserver.annotation.CrossOrigin;
import com.yanzhenjie.andserver.annotation.GetMapping;
import com.yanzhenjie.andserver.annotation.PathVariable;
import com.yanzhenjie.andserver.annotation.PostMapping;
import com.yanzhenjie.andserver.annotation.RequestBody;
import com.yanzhenjie.andserver.annotation.RequestMapping;
import com.yanzhenjie.andserver.annotation.RequestMethod;
import com.yanzhenjie.andserver.annotation.RequestParam;
import com.yanzhenjie.andserver.annotation.RestController;
import com.yanzhenjie.andserver.http.multipart.MultipartFile;
import com.yanzhenjie.andserver.util.MediaType;

import java.util.List;
import java.util.Map;
import java.util.TreeSet;

@CrossOrigin(methods = {RequestMethod.POST, RequestMethod.GET})
@RestController
@RequestMapping(path = "/uas/api/api/v1/m")
public class AndAppClientController {


    @GetMapping("/map/vectorGeoJson")
    public List<GeoJson> getVectorGeoJson(@RequestParam("organNo") Integer organNo,
                                                    @RequestParam(value = "deviceCode", required = false) String deviceCode,
                                                    @RequestParam(value = "zones", required = false) String zones) {
        List<GeoJson> geoJsons = OfflineBizService.getInstance().getVectorGeoJson(organNo, deviceCode, zones);
        return geoJsons;
    }

    @PostMapping(value = "/defect/marker")
    public List<DefectMarkerResVO> defectMarker(@RequestBody DefectQueryReqVO reqVo) {
        List<DefectMarkerResVO> vos = OfflineBizService.getInstance().defectMarker(reqVo.getTenantNo(),
                reqVo.getOrganNo(), reqVo.getDeviceCode(), reqVo.getZone(), reqVo.getPatrolTime(), reqVo.getFaultStatus(), reqVo.getFaultNoInStr());
        return vos;
    }


    @GetMapping(value = "/defect/detail/{defectId}")
    public DefectDetailResVO defectDetail(@PathVariable(name = "defectId") int defectId){
        DefectDetailResVO resVo = OfflineBizService.getInstance().defectDetail(defectId);
        return resVo;
    }

    @PostMapping(value = "/defect/statistics")
    public DefectStatisticsResVO defectStatistics(@RequestBody DefectQueryReqVO reqVo){
        DefectStatisticsResVO resVo = OfflineBizService.getInstance().defectStatistics(reqVo.getTenantNo(),
                reqVo.getOrganNo(), reqVo.getDeviceCode(), reqVo.getZone(), reqVo.getPatrolTime(), reqVo.getFaultStatus(), reqVo.getFaultNameInStr());
        return resVo;
    }

    @PostMapping(value = "/defect/edit/{id}")
    public void defectEdit(@PathVariable(name = "id") int id, @RequestBody DefectEditReqVO reqVo){
        reqVo.setId(id);
        OfflineBizService.getInstance().defectEdit(id, reqVo);
    }

    @GetMapping(value = "/defect/condition")
    public DefectTableHeaderConditionResVO defectCondition(@RequestParam(value = "organNo", required = false) Integer organNo){
        DefectQueryReqVO reqVo = new DefectQueryReqVO();
        reqVo.setOrganNo(organNo);
        DefectTableHeaderConditionResVO resVo = OfflineBizService.getInstance().defectCondition(reqVo.getOrganNo(), reqVo.getZone(), reqVo.getPatrolTime());
        return resVo;
    }


    @PostMapping(value = "/defect/page")
    public PageInfo<StationDefectResVO> defectPage(@RequestBody DefectQueryReqVO reqVo,
                                                             @RequestParam("pageNo") Integer pageNo,
                                                             @RequestParam("pageSize") Integer pageSize) {
        if(null != pageNo){
            reqVo.setPageNo(pageNo);
        }
        if(null != pageSize){
            reqVo.setPageSize(pageSize);
        }
        PageInfo<StationDefectResVO> pageInfo = OfflineBizService.getInstance().defectPage(reqVo.getTenantNo(),
                reqVo.getOrganNo(), reqVo.getDeviceCode(), reqVo.getZone(), reqVo.getPatrolTime(), reqVo.getFaultStatus(), reqVo.getFaultNameInStr(), pageNo, pageSize);
        return pageInfo;
    }


    @PostMapping(value = "/defect/infrared/image/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<Map<String, String>> defectInfraredImageUpload(@RequestParam("organNo") Integer organNo,
                                                                         @RequestParam("defectId") Integer defectId,
                                                                         @RequestParam(name = "file") MultipartFile file){
        List<Map<String, String>> res = OfflineBizService.getInstance().defectInfraredImageUpload(file, organNo, defectId);
        return res;
    }

    @GetMapping(value = "/station/{organNo}/zones")
    public TreeSet<String> stationZones(@PathVariable(name = "organNo") int organNo,
                                                  @RequestParam(value = "patrolTime", required = false) String patrolTime) {
        TreeSet<String> vos = OfflineBizService.getInstance().stationZones(organNo, patrolTime);
        return vos;
    }

    @PostMapping(value = "/defect/image/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<Map<String, String>> defectImageUpload(@RequestParam("organNo") Integer organNo,
                                                                 @RequestParam(value = "defectId", required = false) Integer defectId,
                                                                 @RequestParam(name = "file") MultipartFile file){
        List<Map<String, String>> res = OfflineBizService.getInstance().defectImageUpload(file, organNo, defectId);
        return res;
    }

    @PostMapping(value = "/defect/add")
    public DefectResVO addDefect(@RequestBody DefectAddReqVO reqVo){
        DefectResVO resVo = OfflineBizService.getInstance().addDefect(reqVo);
        return resVo;
    }


    @PostMapping(value = "/draw/device/{organNo}")
    public Long drawDevice(@PathVariable(name = "organNo") int organNo, @RequestBody StationSimulateMapDrawSelectionReqVO reqVo){
        reqVo.setOrganNo(organNo);
        Long gid = OfflineBizService.getInstance().drawDevice(reqVo);
        return gid;
    }


}
